<?php
namespace app\index\controller;
use app\index\model\Staffs;
use app\index\model\Menus;
use app\index\model\Index as M;
/**
 * Created by Submint Text.
 * User: hetaoren<452649510@qq.com>
 * Date: 2017/03/10
 * Explain: 首页控制器
 */
class Index extends Base{
	/**
	 * 跳去登录页
	 */
	public function login(){
		return $this->fetch("/login");
	}
	
    public function index(){
        $this->assign("staffRoleId",session("HTR_STAFF.staffRoleId"));
        model('Remind')->toUpdate();
        $this->assign("checkRead",model('Remind')->checkRead());
    	$m = new Menus();
    	$ms = $m->getMenus();
    	$this->assign("menus",$ms);
    	return $this->fetch("/index");
    }
    
    
    /**
     * 获取子菜单
     */
    public function getSubMenus(){
    	$m = new Menus();
    	return $m->getSubMenus((int)Input('post.id'));
    }
    
    /**
     * 登录验证
     */
    public function checkLogin(){
    	$m = new Staffs();
    	return $m->checkLogin();
    }
    
    /**
     * 退出系统
     */
    public function logout(){
    	session('HTR_STAFF',null);
    	return HTRReturn("", 1);
    }
    
    /**
     * 系统预览
     */
    public function main(){
    	$m = new M();
    	$rs = $m->summary();
    	$this->assign("object",$rs);
    	return $this->fetch("/main");
    }
    
    /**
     * 获取用户权限
     */
    public function getGrants(){
    	$rs = session('HTR_STAFF');
    	if(empty($rs))return HTRReturn("您未登录，请先登录系统");
    	$rs = $rs['privileges'];
    	$grants = [];
    	foreach ($rs as $v){
    		$grants[$v] = true;
    	}
    	return HTRReturn("权限加载成功",1, $grants);
    }
    /**
     * 清除缓存
     */
    public function clearcache(){
    	$m = new M();
    	$rs = $m->clearCache();
    	if($rs){
    		return HTRReturn("清除成功!", 1);
    	}else{
    		return HTRReturn("清除失败 !");
    	}
    }
    /**
     * 发送验证码
     */
    public function sendSms(){
        $phone = input("post.phone");
        $verify = input("post.verify");
        if(trim($phone)==''){
            return HTRReturn("手机号码不能为空！");
        }
        if(trim($verify)==''){
            return HTRReturn("图形验证码不能为空！");
        }
        if(!HTRIsPhone($phone)){
            return HTRReturn("手机号格式不正确！");
        }
        if(!HTRVerifyCheck($verify)){
            return HTRReturn('图形验证码错误!');
        }
        $count = model('staffs')->where(['dataFlag'=>1,'staffPhone'=>$phone])->count();
        if($count<1){
            return HTRReturn('手机号码不存在！');
        }
        $code = rand(100000,999999);
        return model('LogSms')->sendSMS(58860,$phone,$code);
    }

    /**
     * 找回密码
     */
    public function forgetPass(){                
        $phone = input("post.phone");   
        $pass = input("post.pass");   
        $code = input("post.code");
        if(trim($phone)==''){
            return HTRReturn("手机号码不能为空！");
        }
        if(trim($pass)==''){
            return HTRReturn("新密码不能为空！");
        }
        if(!HTRCheckPass($pass)){
            return HTRReturn("密码为6~18位的字母数字");
        }
        $rv = model('LogSms')->checkSMS($phone,$code);
        if($rv["status"]==-1){
            return HTRReturn('短信验证码错误');
        }
        $staff = model('Staffs')->where(['staffPhone'=>$phone,'dataFlag'=>1])->field('secretKey')->find();
        if(empty($staff))return HTRReturn("修改失败");
        $staff->loginPwd = md5($pass.$staff["secretKey"]);
        $result = $staff->save();
        if(false !== $result){
            return HTRReturn("修改成功，请重新登录", 1);
        }else{
            return HTRReturn($this->getError());
        }
    }
}
